<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGoodsTables extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		$prefix = config('ibrand.app.database.prefix', 'ibrand_');
		if (!Schema::hasTable($prefix . 'base_goods')) {
			Schema::create($prefix . 'base_goods', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('商品名称');
				$table->string('goods_no')->comment('商品编号');
				$table->string('goods_barcode')->nullable()->comment('商品条码');
				$table->unsignedInteger('brand_id')->comment('品牌id');
				$table->unsignedInteger('model_id')->comment('base_model表id');
				$table->unsignedInteger('supplier_id')->comment('供应商');
				$table->unsignedInteger('style_id')->nullable()->comment('款式id');
				$table->unsignedInteger('season_id')->nullable()->comment('季节id');
				$table->string('year', 50)->nullable()->comment('年份：2011上');
				$table->unsignedTinyInteger('gender')->default(0)->comment('0:同款 1:男款 2:女款');
				$table->decimal('factory_price')->nullable()->comment('采购价');
				$table->decimal('sell_price')->comment('零售价');
				$table->decimal('market_price')->comment('吊牌价');
				$table->string('img')->nullable()->comment('商品图片');
				$table->unsignedTinyInteger('status')->default(1)->comment('状态 0:下架 1:上架');
				$table->longText('content')->nullable()->comment('详细描述');
				$table->string('unit', 10)->nullable()->comment('单位');
				$table->unsignedInteger('sort')->default(99)->comment('排序');
				$table->string('remark')->nullable()->comment('备注');
				$table->timestamps();
				$table->softDeletes();

				$table->unique('goods_no');
			});
		}

		if (!Schema::hasTable($prefix . 'base_goods_photo')) {
			Schema::create($prefix . 'base_goods_photo', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('goods_id')->comment('商品id');
				$table->string('url')->comment('图片路径');
				$table->unsignedInteger('sort')->default(0);
				$table->string('code');
				$table->unsignedTinyInteger('is_default')->default(0)->comment('默认图片 1:是 0:否');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_goods_product')) {
			Schema::create($prefix . 'base_goods_product', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('goods_id')->comment('商品id');
				$table->string('sku')->comment('单品条码');
				$table->decimal('sell_price')->comment('零售价');
				$table->decimal('market_price')->comment('吊牌价');
				$table->text('specID');
				$table->unsignedTinyInteger('is_show')->default(1);
				$table->timestamps();

				$table->unique('sku');
			});
		}

		if (!Schema::hasTable($prefix . 'base_attribute')) {
			Schema::create($prefix . 'base_attribute', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('model_id')->comment('base_model表id');
				$table->unsignedTinyInteger('type');
				$table->string('name')->comment('属性名称');
				$table->text('value')->comment('属性值');
				$table->unsignedTinyInteger('is_search')->default(0);
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_goods_attribute_relation')) {
			Schema::create($prefix . 'base_goods_attribute_relation', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('goods_id')->comment('商品id');
				$table->unsignedInteger('attribute_id')->comment('base_attribute表id');
				$table->unsignedInteger('model_id')->comment('base_model表id');
				$table->unsignedInteger('attribute_value_id')->default(0)->comment('base_attribute_value表id');
				$table->string('attribute_value')->nullable();
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_attribute_value')) {
			Schema::create($prefix . 'base_attribute_value', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('attribute_id')->comment('base_attribute表id');
				$table->string('name')->comment('属性名称');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_goods_category')) {
			Schema::create($prefix . 'base_goods_category', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('goods_id')->comment('商品id');
				$table->unsignedInteger('category_id')->comment('分类id');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_model')) {
			Schema::create($prefix . 'base_model', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('模型名称');
				$table->string('spec_ids')->nullable();
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_model_attribute_relation')) {
			Schema::create($prefix . 'base_model_attribute_relation', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('model_id')->comment('base_model表id');;
				$table->unsignedInteger('attribute_id')->comment('base_attribute表id');
				$table->timestamps();
			});
		}

		if (!Schema::hasTable($prefix . 'base_spec')) {
			Schema::create($prefix . 'base_spec', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('名称');
				$table->text('value')->nullable();
				$table->unsignedTinyInteger('type')->default(1);
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_goods_spec_relation')) {
			Schema::create($prefix . 'base_goods_spec_relation', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('goods_id')->comment('商品id');
				$table->unsignedInteger('spec_id')->comment('base_spec表id');
				$table->unsignedInteger('spec_value_id')->default(0)->comment('base_specs_value表id');
				$table->string('alias')->nullable();
				$table->string('img')->nullable();
				$table->unsignedInteger('sort')->default('99');
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_specs_value')) {
			Schema::create($prefix . 'base_specs_value', function (Blueprint $table) {
				$table->increments('id');
				$table->unsignedInteger('spec_id');
				$table->string('name');
				$table->string('rgb')->nullable();
				$table->string('color')->nullable();
				$table->unsignedTinyInteger('status')->default(1);
				$table->timestamps();
				$table->softDeletes();
			});
		}

		if (!Schema::hasTable($prefix . 'base_brand')) {
			Schema::create($prefix . 'base_brand', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('品牌名称');
				$table->string('description')->nullable()->comment('描述');
				$table->unsignedInteger('is_show')->default(1);
				$table->string('sort')->default(99)->comment('排序');
				$table->string('url')->nullable();
				$table->string('logo')->nullable()->comment('品牌logo');
				$table->unsignedInteger('discount')->default(100)->comment('默认销售折扣');
				$table->timestamps();
			});
		}

		if (!Schema::hasTable($prefix . 'base_supplier')) {
			Schema::create($prefix . 'base_supplier', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('供应商名称');
				$table->string('code')->comment('供应商编码');
				$table->unsignedTinyInteger('status')->default(1)->comment('状态 1:启用 0:失效');
				$table->string('company_name')->nullable()->comment('公司名称');
				$table->string('company_number')->nullable();
				$table->string('company_address')->nullable()->comment('地址');
				$table->string('company_phone')->nullable()->comment('联系电话');
				$table->string('company_limit')->nullable();
				$table->string('company_scope')->nullable();
				$table->string('company_license')->nullable()->comment('营业执照');
				$table->string('company_permit')->nullable()->comment('营业许可证');
				$table->timestamps();
			});
		}

		if (!Schema::hasTable($prefix . 'base_style')) {
			Schema::create($prefix . 'base_style', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('款式名称');
				$table->timestamps();
			});
		}

		if (!Schema::hasTable($prefix . 'base_season')) {
			Schema::create($prefix . 'base_season', function (Blueprint $table) {
				$table->increments('id');
				$table->string('name')->comment('季节名称');
				$table->timestamps();
			});
		}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		$prefix = config('ibrand.app.database.prefix', 'ibrand_');

		Schema::dropIfExists($prefix . 'base_goods');
		Schema::dropIfExists($prefix . 'base_goods_product');
		Schema::dropIfExists($prefix . 'base_goods_photo');
		Schema::dropIfExists($prefix . 'base_attribute');
		Schema::dropIfExists($prefix . 'base_goods_attribute_relation');
		Schema::dropIfExists($prefix . 'base_attribute_value');
		Schema::dropIfExists($prefix . 'base_goods_category');
		Schema::dropIfExists($prefix . 'base_model');
		Schema::dropIfExists($prefix . 'base_model_attribute_relation');
		Schema::dropIfExists($prefix . 'base_spec');
		Schema::dropIfExists($prefix . 'base_goods_spec_relation');
		Schema::dropIfExists($prefix . 'base_specs_value');
		Schema::dropIfExists($prefix . 'base_brand');
		Schema::dropIfExists($prefix . 'base_supplier');
		Schema::dropIfExists($prefix . 'base_style');
		Schema::dropIfExists($prefix . 'base_season');
	}
}
